<template>
    <h1>借款信息添加页面</h1>

    <table>
        <tr>
            <td>借款款项名称</td>
            <td>
                <input type="text" v-model="AddBorrow.BM_Name" placeholder="请输入借款款项名称" >
            </td>
        </tr>

        <tr>
            <td>借款最高金额</td>
            <td>
                <input type="text" v-model="AddBorrow.BM_Money" placeholder="请输入借款最高金额" >
            </td>
        </tr>

        <tr>
            <td>借款最高年限</td>
            <td>
                <input type="text" v-model="AddBorrow.BM_Year" placeholder="请输入借款最高年限" >
            </td>
        </tr>

        <tr>
            <td>利息</td>
            <td>
                <select v-model="AddBorrow.BI_Id">
                    <option v-for="item in Interest" :key="item.BI_Id" :value="item.BI_Id">
                    {{ item.BI_Year }}年利息{{ item.BI_Interest }}%
                    </option>
                </select>
            </td>
        </tr>

        <tr>
            <td>借款款项简介</td>
            <td>
                <textarea v-model="AddBorrow.BM_Info">

                </textarea>
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input type="button" value="添加" @click="AddData" > 
            </td>
        </tr>

    </table>

</template>

<script setup lang="ts">
import { ref,onMounted } from 'vue';
import axios from 'axios';
//import { useRouter } from 'vue-router';

//const router = useRouter();

//用于绑定的响应式数据
const Interest = ref([{
    BI_Id:2,
    BI_Year:'',
    BI_Interest:'',
}])

//用于添加的响应式数据
const AddBorrow = ref({
    BM_Id:'',
    BM_Name:'',
    BM_Money:'',
    BM_Year:'',
    BM_Info:'',
    BI_Id:'',
})

onMounted(()=>{
    Showinfo();
})

const Showinfo = ()=>{
    axios({
        url:'http://localhost:53279/api/Borrow/Server',
        method:'get',
    })
    .then(function(res){
        console.log(res.data);
        Interest.value = res.data;
    })
    .catch(function(err){
        console.log(err);
    })
}

const AddData = ()=>{
    axios({
        url:'http://localhost:53279/api/Borrow/AddBorrow',
        method:'post',
        data:{
            
            BM_Name:AddBorrow.value.BM_Name,
            BM_Money:AddBorrow.value.BM_Money,
            BM_Year:AddBorrow.value.BM_Year,
            BI_Id:AddBorrow.value.BI_Id,
            BM_Info:AddBorrow.value.BM_Info,
        }
    })
    .then(function(res){
        console.log(res.data);
        if(res.data==1){
            alert('添加成功');

        }
        else if(res.data==-1){
            alert('借款项目名称已存在,请重新输入');
            return;
        }
        else{
            alert('添加失败');
            return;
        }
    })
    .catch(function(err){
        console.log(err);
    })
}

</script>